﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Model.Interfaces;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.School")]
    public partial class School : AbstractMappingClass, INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idSchool;

        private string _nameSchool;

        private string _schoolMainPhoneNumber;

        private string _schoolAdditionalPhoneNumber;

        private string _contact;

        private string _fax;

        private System.Nullable<short> _refAddress;

        private System.Nullable<short> _refSchoolStatus;

        private string _additionalInformation;

        private string _email;

        private string _webSite;

        private EntitySet<Student> _Students;

        private EntityRef<Address> _Address;

        private EntityRef<SchoolStatus> _SchoolStatus;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnidSchoolChanging(short value);
        partial void OnidSchoolChanged();
        partial void OnnameSchoolChanging(string value);
        partial void OnnameSchoolChanged();
        partial void OnschoolMainPhoneNumberChanging(string value);
        partial void OnschoolMainPhoneNumberChanged();
        partial void OnschoolAdditionalPhoneNumberChanging(string value);
        partial void OnschoolAdditionalPhoneNumberChanged();
        partial void OncontactChanging(string value);
        partial void OncontactChanged();
        partial void OnrefAddressChanging(System.Nullable<short> value);
        partial void OnrefAddressChanged();
        partial void OnrefSchoolStatusChanging(System.Nullable<short> value);
        partial void OnrefSchoolStatusChanged();
        partial void OnadditionalInformationChanging(string value);
        partial void OnadditionalInformationChanged();
        partial void OnwebSiteChanging(string value);
        partial void OnwebSiteChanged();
        #endregion

        public School()
        {
            this._Students = new EntitySet<Student>(new Action<Student>(this.attach_Students), new Action<Student>(this.detach_Students));
            this._Address = default(EntityRef<Address>);
            this._SchoolStatus = default(EntityRef<SchoolStatus>);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idSchool", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idSchool
        {
            get
            {
                return this._idSchool;
            }
            set
            {
                if ((this._idSchool != value))
                {
                    this.OnidSchoolChanging(value);
                    this.SendPropertyChanging();
                    this._idSchool = value;
                    this.SendPropertyChanged("idSchool");
                    this.OnidSchoolChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_nameSchool", DbType = "VarChar(255)")]
        public string nameSchool
        {
            get
            {
                return this._nameSchool;
            }
            set
            {
                if ((this._nameSchool != value))
                {
                    this.OnnameSchoolChanging(value);
                    this.SendPropertyChanging();
                    this._nameSchool = value;
                    this.SendPropertyChanged("nameSchool");
                    this.OnnameSchoolChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_schoolMainPhoneNumber", DbType = "VarChar(25)")]
        public string schoolMainPhoneNumber
        {
            get
            {
                return this._schoolMainPhoneNumber;
            }
            set
            {
                if ((this._schoolMainPhoneNumber != value))
                {
                    this.OnschoolMainPhoneNumberChanging(value);
                    this.SendPropertyChanging();
                    this._schoolMainPhoneNumber = value;
                    this.SendPropertyChanged("schoolMainPhoneNumber");
                    this.OnschoolMainPhoneNumberChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_schoolAdditionalPhoneNumber", DbType = "VarChar(25)")]
        public string schoolAdditionalPhoneNumber
        {
            get
            {
                return this._schoolAdditionalPhoneNumber;
            }
            set
            {
                if ((this._schoolAdditionalPhoneNumber != value))
                {
                    this.OnschoolAdditionalPhoneNumberChanging(value);
                    this.SendPropertyChanging();
                    this._schoolAdditionalPhoneNumber = value;
                    this.SendPropertyChanged("schoolAdditionalPhoneNumber");
                    this.OnschoolAdditionalPhoneNumberChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_contact", DbType = "VarChar(200)")]
        public string contact
        {
            get
            {
                return this._contact;
            }
            set
            {
                if ((this._contact != value))
                {
                    this.OncontactChanging(value);
                    this.SendPropertyChanging();
                    this._contact = value;
                    this.SendPropertyChanged("contact");
                    this.OncontactChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refAddress", DbType = "SmallInt")]
        public System.Nullable<short> refAddress
        {
            get
            {
                return this._refAddress;
            }
            set
            {
                if ((this._refAddress != value))
                {
                    if (this._Address.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefAddressChanging(value);
                    this.SendPropertyChanging();
                    this._refAddress = value;
                    this.SendPropertyChanged("refAddress");
                    this.OnrefAddressChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refSchoolStatus", DbType = "SmallInt")]
        public System.Nullable<short> refSchoolStatus
        {
            get
            {
                return this._refSchoolStatus;
            }
            set
            {
                if ((this._refSchoolStatus != value))
                {
                    if (this._SchoolStatus.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefSchoolStatusChanging(value);
                    this.SendPropertyChanging();
                    this._refSchoolStatus = value;
                    this.SendPropertyChanged("refSchoolStatus");
                    this.OnrefSchoolStatusChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_additionalInformation", DbType = "text")]
        public string additionalInformation
        {
            get
            {
                return this._additionalInformation;
            }
            set
            {
                if ((this._additionalInformation != value))
                {
                    this.OnadditionalInformationChanging(value);
                    this.SendPropertyChanging();
                    this._additionalInformation = value;
                    this.SendPropertyChanged("additionalInformation");
                    this.OnadditionalInformationChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_webSite", DbType = "varchar(250)")]
        public string webSite
        {
            get
            {
                return this._webSite;
            }
            set
            {
                if ((this._webSite != value))
                {
                    this.OnwebSiteChanging(value);
                    this.SendPropertyChanging();
                    this._webSite = value;
                    this.SendPropertyChanged("webSite");
                    this.OnwebSiteChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_fax", DbType = "varchar(100)")]
        public string fax
        {
            get
            {
                return _fax;
            }
            set
            {
                if ((_fax != value))
                {
                    this.OnwebSiteChanging(value);
                    this.SendPropertyChanging();
                    _fax = value;
                    SendPropertyChanged("fax");
                    this.OnwebSiteChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_email", DbType = "varchar(100)")]
        public string email
        {
            get { return _email; }
            set
            {
                if(_email != value)
                {
                    SendPropertyChanging();
                    _email = value;
                    SendPropertyChanged("email");
                }
            }
        }

        [AssociationAttribute(Name = "School_Student", Storage = "_Students", ThisKey = "idSchool", OtherKey = "refSchool")]
        public EntitySet<Student> Students
        {
            get
            {
                return this._Students;
            }
            set
            {
                this._Students.Assign(value);
            }
        }

        [AssociationAttribute(Name = "Address_School", Storage = "_Address", ThisKey = "refAddress", OtherKey = "idAddress", IsForeignKey = true)]
        public Address Address
        {
            get
            {
                return _Address.Entity;
            }
            set
            {
                Address previousValue = this._Address.Entity;
                if (((previousValue != value)
                     || (this._Address.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Address.Entity = null;
                        previousValue.Schools.Remove(this);
                    }
                    this._Address.Entity = value;
                    if ((value != null))
                    {
                        value.Schools.Add(this);
                        this._refAddress = value.idAddress;
                    }
                    else
                    {
                        this._refAddress = default(Nullable<short>);
                    }
                    this.SendPropertyChanged("Address");
                }
            }
        }

        [AssociationAttribute(Name = "SchoolStatus_School", Storage = "_SchoolStatus", ThisKey = "refSchoolStatus", OtherKey = "idSchoolStatus", IsForeignKey = true)]
        public SchoolStatus SchoolStatus
        {
            get
            {
                return this._SchoolStatus.Entity;
            }
            set
            {
                SchoolStatus previousValue = this._SchoolStatus.Entity;
                if (((previousValue != value)
                     || (this._SchoolStatus.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._SchoolStatus.Entity = null;
                        previousValue.Schools.Remove(this);
                    }
                    this._SchoolStatus.Entity = value;
                    if ((value != null))
                    {
                        value.Schools.Add(this);
                        this._refSchoolStatus = value.idSchoolStatus;
                    }
                    else
                    {
                        this._refSchoolStatus = default(Nullable<short>);
                    }
                    this.SendPropertyChanged("SchoolStatus");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void attach_Students(Student entity)
        {
            this.SendPropertyChanging();
            entity.School = this;
        }

        private void detach_Students(Student entity)
        {
            this.SendPropertyChanging();
            entity.School = null;
        }

        public override Model.ModelClasses.AbstractModel PureModel
        {
            get
            {
                return base.PureModel;
            }
            set
            {
                if (_pureModel != null)
                    _pureModel.UpdateModelEvent -= UpdateModelEventHandler;

                if (value != null)
                    value.UpdateModelEvent += UpdateModelEventHandler;

                _pureModel = value;
            }
        }

        private void UpdateModelEventHandler(object sender, EventArgs e)
        {
            var modelSchool = ((ISchool) _pureModel);
            nameSchool = modelSchool.Name;
            additionalInformation = modelSchool.AdditionalInformation;
            if (modelSchool.ContactInformation != null)
            {
                schoolMainPhoneNumber = modelSchool.ContactInformation.LandlinePhone;
                schoolAdditionalPhoneNumber = modelSchool.ContactInformation.AdditionalPhone;
                email = modelSchool.ContactInformation.Email;
                contact =
                    ((Model.ModelClasses.OrganizationContactInformation) modelSchool.ContactInformation).ContactPerson;
                fax = ((Model.ModelClasses.OrganizationContactInformation) modelSchool.ContactInformation).Fax;
            }
            UpdateData();
        }
    }
}